package com.cogent.business.helper;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.BussinessInstance;
import com.cogent.dao.BussinessQueryHelper;
import com.cogent.dao.facade.BussinessDaoFacade;

public class OrganHelper {
	
	public static String getQueryOrganSelect(HttpServletRequest request, HashMap formMap, String name) {
		String curvalue = MapUtil.getStringValue(formMap, name);
		BussinessInstance bussinessInstance = new BussinessInstance();
		BussinessDaoFacade bussinessFacade = bussinessInstance.getBussinessFacade();
		String sql = "select treecode as enumvalue, organname as enumoption, treelevel as enumlevel from sys_organ order by organcode asc, treelevel asc";
		List list = BussinessQueryHelper.query("sys_organ", sql);
		StringBuffer str = new StringBuffer();
		int level = 0;
		str.append("<select name=\"").append(name).append("\" id=\"").append(name).append("\">");
		str.append("<option value=\"\">请选择</option>");
		Iterator it = list.iterator();
		while(it.hasNext()) {
			HashMap dataMap = (HashMap)it.next();
			str.append(HtmlUIHelper.getOption(MapUtil.getStringValue(dataMap, "enumoption"), MapUtil.getStringValue(dataMap, "enumvalue")
						, MapUtil.getStringValue(formMap, name), MapUtil.getIntValue(dataMap, "enumlevel") - level));
		}
		str.append("</select>");
		
		return str.toString();
	}
	
	public static String getQueryOrganInput(HttpServletRequest request, HashMap formMap, String name) {
		String curvalue = MapUtil.getStringValue(formMap, name);
		BussinessInstance bussinessInstance = new BussinessInstance();
		BussinessDaoFacade bussinessFacade = bussinessInstance.getBussinessFacade();
		String sql = "select id as enumvalue, organname as enumoption, treelevel as enumlevel from sys_organ order by organcode asc, treelevel asc";
		List list = BussinessQueryHelper.query("sys_organ", sql);
		StringBuffer str = new StringBuffer();
		int level = 0;
		str.append("<select name=\"").append(name).append("\" id=\"").append(name).append("\">");
		str.append("<option value=\"\">请选择</option>");
		Iterator it = list.iterator();
		while(it.hasNext()) {
			HashMap dataMap = (HashMap)it.next();
			str.append(HtmlUIHelper.getOption(MapUtil.getStringValue(dataMap, "enumoption"), MapUtil.getStringValue(dataMap, "enumvalue")
						, MapUtil.getStringValue(formMap, name), MapUtil.getIntValue(dataMap, "enumlevel") - level));
		}
		str.append("</select>");
		
		return str.toString();
	}
	
	public static String getOrgUserSelect(HttpServletRequest request, HashMap formMap, String name) {
		String curvalue = MapUtil.getStringValue(formMap, name);
		BussinessInstance bussinessInstance = new BussinessInstance();
		BussinessDaoFacade bussinessFacade = bussinessInstance.getBussinessFacade();
		String sql = "select id as enumvalue, realname as enumoption, 1 as enumlevel from sys_user where status=0";
		List list = BussinessQueryHelper.query("sys_user", sql);
		StringBuffer str = new StringBuffer();
		int level = 0;
		str.append("<select name=\"").append(name).append("\" id=\"").append(name).append("\">");
		str.append("<option value=\"\">请选择</option>");
		Iterator it = list.iterator();
		while(it.hasNext()) {
			HashMap dataMap = (HashMap)it.next();
			str.append(HtmlUIHelper.getOption(MapUtil.getStringValue(dataMap, "enumoption"), MapUtil.getStringValue(dataMap, "enumvalue")
						, MapUtil.getStringValue(formMap, name), MapUtil.getIntValue(dataMap, "enumlevel") - level));
		}
		str.append("</select>");
		
		return str.toString();
	}
	
	public static String getOrgUserName(String userid) {
		if(StringUtil.isEmpty(userid))
			return "";
		HashMap datamap = BussinessQueryHelper.getValueById("sys_user", userid);
		
		return MapUtil.getStringValue(datamap, "realname");
	}
	
	public static String getOrganName(String orgid) {
		HashMap datamap = BussinessQueryHelper.getValueById("sys_organ", orgid);
		
		return MapUtil.getStringValue(datamap, "organname");
	}

}
